package org.example.repository;

import org.example.domain.EsDiscussion;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Highlight;
import org.springframework.data.elasticsearch.annotations.HighlightField;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

//该接口继承了ElasticsearchRepository接口，ElasticsearchRepository接口定义了Elasticsearch的CRUD
@Repository
public interface ESDiscussionRepository extends ElasticsearchRepository<EsDiscussion, String> {
//    @Highlight(fields = {
//            @HighlightField(name = "title"),
//            @HighlightField(name = "type"),
//            @HighlightField(name = "content")
//    })
//    @Query( "{" +
//                "\"from\": ?2," +
//                "\"size\": ?1, " +
//                "\"query\": {" +
//                    "\"multi_match\": {" +
//                        "\"query\": \"?0\"," +
//                        "\"fields\": [\"title\", \"content\",\"type\"]" +
//                    "}" +
//                "}" +
//            "}")
//    SearchHits<EsDiscussion> findDiscussionByKey(String key, int size, int from);

    @Highlight(fields = {
            @HighlightField(name = "title"),
            @HighlightField(name = "type"),
            @HighlightField(name = "content")
    })
    Page<EsDiscussion> findByTitleOrContentOrType(String title, String content, String type, Pageable pageable);


    //根据title和description查询产品,注意函数名称里面的名称一定要和属性名称一致，否则会报错
//    List<EsDiscussion> findByTitleOrDescription(String title, String description);

}